package com.hxzy.service.impl;


import cn.hutool.json.JSONUtil;
import com.hxzy.entity.DishFlavor;
import com.hxzy.entity.Merchandise;
import com.hxzy.mapper.MerchandiseMapper;
import com.hxzy.service.MerchandiseService;
import com.hxzy.dto.back.MerchandiseSearchDTO;
import com.hxzy.vo.back.DishFlavorProperty;
import com.hxzy.vo.back.DishFlavorVO;
import com.hxzy.vo.back.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;


/**
* @Author wy
* @Description 商品信息业务逻辑
* @Date 2023-04-18
* @Version 1.0
*/
@Service
public class MerchandiseServiceImpl extends ServiceImpl<Merchandise,BigDecimal>  implements MerchandiseService {

    private MerchandiseMapper  merchandiseMapper;

    @Autowired
    public void setMerchandiseMapper(MerchandiseMapper merchandiseMapper) {
        this.merchandiseMapper = merchandiseMapper;
        super.setMyBatisBaseDao(merchandiseMapper);
    }

    @Override
    public List<ProductVO> search(MerchandiseSearchDTO searchDTO) {
        //根据条件查询商品信息
        List<ProductVO> productVOList = this.merchandiseMapper.search(searchDTO);

        for(ProductVO productVO: productVOList){
           //数据库中的口味列表
            List<DishFlavor> dishFlavorList = productVO.getDishFlavorList();
            if(!dishFlavorList.isEmpty()){
                //转换成前端所需要的数据格式 {"name:"", value: [] }
               List<DishFlavorVO> voList=new ArrayList<>();

               for(DishFlavor dishFlavor : dishFlavorList){
                   DishFlavorVO vo=new DishFlavorVO();
                   vo.setName(dishFlavor.getName());

                   //dishFlavor.getValue() = [{"value": "正常冰","price": 0},{"value": "少冰","price": 0},{"value": "常温","price": 0},{"value": "温热","price": 0}]
                   //转换成  List<DishFlavorProperty> ,  Hutool工具
                   List<DishFlavorProperty> dishFlavorPropertyList = JSONUtil.toList(dishFlavor.getValue(), DishFlavorProperty.class);
                   vo.setValue(dishFlavorPropertyList);
                   voList.add(vo);
               }

               productVO.setDishFlavorVOList(voList);
            }
        }

        return productVOList;
    }

}
